//
//  TransitionNavController.swift
//  SwiftLearningDemo
//
//  Created by ShareAnimation on 17/6/5.
//  Copyright © 2017年 爱丽丝的梦境. All rights reserved.
//

import UIKit
import SnapKit
import FXBlurView
import Shimmer

class TransitionNav: UINavigationController, UIViewControllerTransitioningDelegate {
    override init(nibName nibNameOrNil: String?, bundle nibBundleOrNil: Bundle?) {
        super.init(nibName: nil, bundle: nil)
    }
    init(view:UIView) {
        let vc = TransitionNavController()
        vc.underlyingView = view
        super.init(rootViewController: vc)
        self.transitioningDelegate = self
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    func animationController(forPresented presented: UIViewController, presenting: UIViewController, source: UIViewController) -> UIViewControllerAnimatedTransitioning? {
        return ViewControllerTranstionPresent()
    }
}

class ViewControllerTranstionPresent: NSObject, UIViewControllerAnimatedTransitioning {
    func transitionDuration(using transitionContext: UIViewControllerContextTransitioning?) -> TimeInterval {
        return 1
    }
    func animateTransition(using transitionContext: UIViewControllerContextTransitioning) {
        let toVC = transitionContext.viewController(forKey: UITransitionContextViewControllerKey.to) as! TransitionNav
        let contrainer = transitionContext.containerView
        contrainer.addSubview(toVC.view)
        toVC.view.alpha = 0
        
        UIView.animate(withDuration: transitionDuration(using: transitionContext), animations: { 
            toVC.view.alpha = 1
        }) { (finished: Bool) in
//一定要执行这句代码，否者viewController的viewDidLoad不执行
                transitionContext.completeTransition(!transitionContext.transitionWasCancelled)
        }
    }
}

class TransitionNavController: UIViewController,UITableViewDelegate,UITableViewDataSource {
    
    fileprivate var _tableView: UITableView!
    fileprivate var tableView: UITableView {
        get  {
            if _tableView != nil {
                return _tableView
            }
            _tableView = UITableView()
            _tableView.separatorStyle = .none
            
            _tableView.backgroundColor = UIColor.clear
            _tableView.contentInset = UIEdgeInsetsMake(64, 0, 0, 0)//让tableView在navigationBar下
            _tableView.register(UITableViewCell.self, forCellReuseIdentifier: "cell")
            _tableView.delegate = self
            _tableView.dataSource = self
            return _tableView
        }
    }

    var frostedView = FXBlurView()
    
    var underlyingView: UIView?
    
    override func viewDidLoad() {
        super.viewDidLoad()
//        self.view.backgroundColor = UIColor.white
        
        frostedView.underlyingView = self.underlyingView!
        frostedView.isDynamic = false
        frostedView.blurRadius = 35
        frostedView.tintColor = UIColor.black
        frostedView.frame = self.view.frame
        self.view.addSubview(frostedView)

        let shimmeringView = FBShimmeringView()
        shimmeringView.isShimmering = true
        shimmeringView.shimmeringOpacity = 0.3
        shimmeringView.shimmeringSpeed = 45
        shimmeringView.shimmeringHighlightLength = 0.6
        self.view.addSubview(shimmeringView)
        let label = UILabel(frame: shimmeringView.frame)
        label.text = "下拉关闭查看"
        label.font = UIFont(name: "HelveticaNeue-Light", size: 12)
        label.textColor = UIColor.black
        label.textAlignment = .center
        label.backgroundColor = UIColor.clear
        shimmeringView.contentView = label
        shimmeringView.frame = CGRect( x: (UIScreen.main.bounds.width-80) / 2 , y: 15, width: 80, height: 44)

        self.view.addSubview(self.tableView);
        self.tableView.snp.makeConstraints{ (make) -> Void in
            make.left.right.equalTo(self.view);
            make.height.equalTo(self.view)
            make.top.equalTo(self.view.snp.bottom)
        }
    }
    
    override func viewDidAppear(_ animated: Bool) {
        super.viewDidAppear(animated)
        self.tableView.snp.remakeConstraints{ (make) -> Void in
            make.left.right.equalTo(self.view);
            make.height.equalTo(self.view)
            make.top.equalTo(self.view).offset(64)
        }
        self.navigationController?.navigationBar.alpha = 0
        UIView.animate(withDuration: 0.6, delay: 0, usingSpringWithDamping: 1, initialSpringVelocity: 8, options: UIViewAnimationOptions.curveLinear, animations: { () -> Void in

            self.view.layoutIfNeeded()
            
            }, completion: nil)
    }
    override func viewWillAppear(_ animated: Bool) {
        super.viewWillAppear(animated)
    }
    
    func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
        return 40
    }
    
    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return 5
    }
    
    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCell(withIdentifier: "cell", for: indexPath)
        cell.backgroundColor = UIColor.white
        cell.textLabel?.text = "1111122222"
        cell.textLabel?.textColor = UIColor.black
        
        return cell
    }
    
    
    func scrollViewDidEndDragging(_ scrollView: UIScrollView, willDecelerate decelerate: Bool) {
        //下拉关闭
        if scrollView.contentOffset.y <= -100 {
            //让scrollView 不弹跳回来
            scrollView.contentInset = UIEdgeInsetsMake(-1 * scrollView.contentOffset.y, 0, 0, 0)
            scrollView.isScrollEnabled = false
            self.navigationController!.dismiss(animated: true, completion: nil)
        }
    }
}
